<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
set_html('title','登录员工');
if($_RQ['op']=='get'){
	$return['set']=$return['pluginset'];
	$return['set']['loginlogo']=tomedia($return['set']['logo']);
	$return['set']['loginbg']=tomedia($return['set']['loginbg']);
	if($_SESSION['os']=='windows'){
		$return['session_id']=session_id();
	}
	if($_SESSION['uid'] && !$_RQ['loginup']){
		exi('','error','jy_yxgoodserpfw/store.index/');
	}
	$_SESSION['workerid']='';
	$_SESSION['logout_forward']='jy_yxgoodserpfw/workerlogin/';
	$_SESSION['forward_url']='jy_yxgoodserpfw/store.index/'; 
	$return['forward_url']=base64_encode($_SESSION['forward_url']);
	$return['info']=array();
	$return['openid']=$_SESSION['openid'];
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	$user=pdo_get('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['username']));
	if(!$user || $post['password']!=base64_decode($user['password'])){
		exi('用户名或密码错误','error');
	}
	if($_SESSION['uid'] && $user['mid']!=$_SESSION['uid']){
		pdo_update('jy_yxw_worker',array('mid'=>$_SESSION['uid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		$user['mid']=$_SESSION['uid'];
	}
	if(!$user['mid']){
		$user['mid']=cfc('member')->register(array('password'=>$post['password']));
		pdo_update('jy_yxw_worker',array('mid'=>$user['mid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
	}
	if(!$user['mid']){
		exi('该商户没有绑定用户/管理员！','error');
	}
	$checkid=pdo_getcolumn('core_members',array('id'=>$user['mid'],'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$checkid){
		$user['mid']=cfc('member')->register(array('password'=>$post['password']));
		pdo_update('jy_yxw_worker',array('mid'=>$user['mid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		//exi('该员工没有绑定的用户已经不存在，请更换！','error');
	}
	$_SESSION['workerid']=$user['id'];
	$_SESSION['uid']=$user['mid'];
	$_SESSION['merchid']=$_SESSION['onemerchid']=$user['id'];
	$_SESSION['member']['username']=$user['username'];
	unset($_SESSION['workertype']);
	//绝对地址，会刷新页面
	if(!$_SESSION['wxapp_id']){
		$url=custom_url('jy_yxgoodserpfw/store.index/',true);
	}else{
		$url='jy_yxgoodserpfw/store.index/';
	}
	exi('登陆成功!','',$url);
}
if($_RQ['op']=='onelogin'){
	if(!$_RQ['upass'] || !$_RQ['uname']){
		exi('请填写账户名和密码','error');
	}
	$user=pdo_get('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'username'=>$_RQ['uname']));
	if(!$user || $_RQ['upass']!=$user['password']){
		exi('用户名或密码错误','error');
	}
	if($_SESSION['uid'] && $user['mid']!=$_SESSION['uid']){
		pdo_update('jy_yxw_worker',array('mid'=>$_SESSION['uid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		$user['mid']=$_SESSION['uid'];
	}
	if(!$user['mid']){
		exi('该员工没有绑定用户/管理员！','error');
	}
	$_SESSION['uid']=$user['mid'];
	$_SESSION['merchid']=$_SESSION['onemerchid']=$user['id'];
	$_SESSION['member']['username']=$user['username'];
	//绝对地址，会刷新页面
	if(!$_SESSION['wxapp_id']){
		$url=custom_url('jy_yxgoodserpfw/store.index/',true);
	}else{
		$url='jy_yxgoodserpfw/store.index/';
	}
	exi('','',$url);
}